package com.joymeter.data.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joymeter.common.utils.EmptyUtils;
import com.joymeter.common.utils.TimeTools;
import com.joymeter.data.dao.DeviceInfoDao;
import com.joymeter.data.entity.DeviceInfo;
import com.joymeter.data.service.DeviceInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @ClassName DeviceInfoServiceImpl
 * @Description TODO
 * deviceInfo表的操作
 * @Author liang
 * @Date 2018/8/12 13:08
 * @Version 1.0
 **/
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoDao,DeviceInfo> implements DeviceInfoService {



    @Override
    public DeviceInfo selectDeviceInfoById(String deviceId) {
        DeviceInfo deviceInfo = baseMapper.selectDeviceInfoById(deviceId);
        return deviceInfo;
    }

    @Override
    public DeviceInfo selectDeviceInfoByValId(String valveId) {
        DeviceInfo deviceInfo = baseMapper.selectDeviceInfoByValId(valveId);
        return deviceInfo;
    }

    /**
     *
     * @param deviceInfo
     * @return
     */
    @Override
    public Integer updateDeviceInfo(DeviceInfo deviceInfo) throws Exception {
        String deviceId = deviceInfo.getDeviceId();
        if(EmptyUtils.isEmpty(deviceId)) throw new Exception("设备编号为空");
        //********状态变化需要更新时间的***********
        String deviceState = deviceInfo.getDeviceState();
        String simState = deviceInfo.getSimState();
        String readState = deviceInfo.getReadState();
        String valveState = deviceInfo.getValveState();
        String dataUsed = deviceInfo.getDataUsed();
        if((!EmptyUtils.isEmpty(simState)) || (!EmptyUtils.isEmpty(readState)) || (!EmptyUtils.isEmpty(valveState)) || (!EmptyUtils.isEmpty(dataUsed)) || (!EmptyUtils.isEmpty(deviceState)&&deviceState.equals("1"))){
            //将时间格式转换成符合Timestamp要求的格式.
            deviceInfo.setUpdateTime(TimeTools.getCurrentDate(new Date()));
        }
        Integer integer = baseMapper.updateDeviceInfo(deviceInfo);
        return integer;

    }

    @Override
    public Integer getDeviceCount(DeviceInfo deviceInfo) {
        Integer deviceCount = baseMapper.getDeviceCount(deviceInfo);
        return deviceCount;
    }

    /**
     * 一天内无数据的设备
     * @return
     */
    @Override
    public List<DeviceInfo> selectNoneDateADay() {
        List<DeviceInfo> list = baseMapper.selectNoneDateADay();
        return list;
    }

    @Override
    public boolean insertDeviceInfo(DeviceInfo deviceInfo) {
        boolean insertOrUpdate = insertOrUpdate(deviceInfo);
        return insertOrUpdate;
    }

    @Override
    public boolean deleteDeviceInfoById(String deviceId) {
        EntityWrapper<DeviceInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("deviceId",deviceId);
        boolean delete = delete(wrapper);
        return delete;
    }
    @Override
    public DeviceInfo selectByRand() {
        DeviceInfo deviceInfo = baseMapper.selectByRand();
        return  deviceInfo;
    }

}
